/*---------------------------------------------------------------------------*/
#include "FilterBox.h"
#include "BrowserBox.h"
#include "Utils.h"
#include <QtGui>
#include <QWidget>
#include <iostream>
#include <QSizePolicy>
#include "MenuButton.h"
/*---------------------------------------------------------------------------*/
using namespace std;
/*---------------------------------------------------------------------------*/
FilterBox::FilterBox( BrowserBox *_flw ) : QWidget()
{
	QHBoxLayout *hlayout = new QHBoxLayout(this);
	
	hlayout->setSpacing(1);	
	
	
	QPushButton *buttonClose = new MenuButton("", "stop.png", 22, 22, "Disable filter");
	QPushButton *buttonGo = new MenuButton("", "search.png", 22, 22, "Enable filter");
	
	
	filterText = new QLineEdit("*", this) ;

	
	
	
	
	
	hlayout->addWidget( new QLabel("Filter : ", this) );
	hlayout->addWidget( filterText );
	hlayout->addWidget( buttonGo );
	hlayout->addWidget( buttonClose );
	
	hlayout->setContentsMargins(0,0,0,0);
	setLayout(hlayout);
	
	connect( buttonGo, SIGNAL( clicked() ), this, SLOT( returnPressed() ) );
	connect( filterText, SIGNAL( returnPressed() ), this, SLOT( returnPressed() ) );
	connect( buttonClose, SIGNAL( clicked() ), this, SLOT( close() ) );
	
	flw = _flw;
	
	
	setVisible(false); // start hidden
}
/*---------------------------------------------------------------------------*/
FilterBox::~FilterBox()
{
	
}
/*---------------------------------------------------------------------------*/
void FilterBox::setFocus( QKeyEvent *event )
{
	setVisible(true);
	filterText->setFocus( Qt::TabFocusReason );
	
	
	if(event)
	{
		unsigned char c = event->key() - ( Qt::Key_A - 'a' );
	
		if( c >= 'a' && c <= 'z' )
		{
			filterText->setText( QString( c ) );
		}
	}
	
	
}
/*---------------------------------------------------------------------------*/
void FilterBox::returnPressed()
{
	//DEBUGMSG("FilterBox::returnPressed()");
	emit changed( filterText->text().toStdString() );
}
/*---------------------------------------------------------------------------*/
void FilterBox::close()
{
	//DEBUGMSG("FilterBox::close()");
	setVisible(false);
	emit closed();
}
/*---------------------------------------------------------------------------*/  
void FilterBox::keyPressEvent( QKeyEvent *event )
{
	
	//DEBUGMSG( "FilterBox::keyPressEvent() : " << event->key() );
	
	if(  event->key() == Qt::Key_Up ||  event->key() == Qt::Key_Down  )
	{
		flw->keyPressEvent( event );
		flw->setFocus();
	}
	else if( event->key() == Qt::Key_Escape )
	{
		close();
	}	
	else if( event->key() == Qt::Key_Escape )
	{
		close();
	}
	else
	{
		
	}
	
	
}
/*---------------------------------------------------------------------------*/
